package com.bizintelapps.zaetoon.ajax;

import com.bizintelapps.zaetoon.domain.Program;
import com.bizintelapps.zaetoon.domain.ReportDto;
import com.bizintelapps.zaetoon.domain.UserProfile;
import com.bizintelapps.zaetoon.security.SpringSecurityContext;
import com.bizintelapps.zaetoon.service.ProgramService;
import com.bizintelapps.zaetoon.service.UserEnrollmentService;
import com.bizintelapps.zaetoon.service.UserProfileService;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

/**
 *
 * @author intesar
 */
public class ProgramAjaxService {

    @Autowired
    protected ProgramService programService;
    @Autowired
    protected UserEnrollmentService enrollmentService;
    @Autowired
    protected UserProfileService userProfileService;
    @SuppressWarnings("unused")
    private static final Logger logger = Logger.getLogger(ProgramAjaxService.class);

    /**
     * 
     */
    public List<Program> upcomingPrograms() {
        String username = SpringSecurityContext.getUsername();
        List<Program> list = programService.getEligiblePrograms(username);
        return list;
    }

    /**
     * Initially No reports are due and enrollment is active
     * @param programId 
     */
    public void enroll(Integer programId) {
        this.enrollmentService.enroll(programId);
    }
    
    /**
     *   When Item is submitted and no other items are active then Enrollment should make isReportDue false
     * 
     *   All items are submitted deactivate enrollment and also publish results
     */
    public String submitSalah(Integer reportId, Integer fajr, Integer zuhr, Integer asr, Integer magrib, Integer isha) {
        return this.enrollmentService.submitSalah(reportId, fajr, zuhr, asr, magrib, isha);
    }
    
    /**
     * 
     * @return 
     *  program name, Day/Days, Date, Total Reports Due, category
     * 
     */
    public List<ReportDto> getReports() {
        return this.enrollmentService.getReports();
    }

    /**
     * 
     * @return 
     */
    public UserProfile getUserProfile() {
        String username = SpringSecurityContext.getUsername();
        return this.userProfileService.findByUsername(username);
    }

    /**
     * 
     * @param title
     * @param firstname
     * @param lastname
     * @param dob
     * @param gender
     * @param city
     * @param country 
     */
    public void saveUserProfile(String title, String firstname, String lastname,
            String dob, String gender, String city, String country) {
        String username = SpringSecurityContext.getUsername();
        UserProfile userProfile = this.userProfileService.findByUsername(username);

        userProfile.setTitle(title);
        userProfile.setFirstName(firstname);
        userProfile.setLastName(lastname);
        userProfile.setCity(city);
        userProfile.setCountry(country);
        userProfile.setGender(gender);


        if (dob.length() == 10) {
            try {
                DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                userProfile.setDob(df.parse(dob));
            } catch (ParseException ex) {
            }
        }

        this.userProfileService.save(userProfile);
    }
}
